<template>
  <div class="app-container">
    <el-card shadow="never" class="filter-bar" >
      <div class="btn-group pull-left">
        <el-button type="success" icon="el-icon-edit" size="mini">添加</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
      </div>
    </el-card>

    <el-card shadow="never" class="store-table" >
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="100">
        </el-table-column>

        <el-table-column
          label="名称">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="created_at"
          label="创建日期">
        </el-table-column>
        <el-table-column
          label="操作"
          width="150">
        <template slot-scope="scope">
          <router-link :to="{name:'products-show', params: {id: 2}}">
            <el-button type="text" size="small">查看</el-button>
          </router-link>
          
          <el-button type="text" size="small">禁用</el-button>

          <router-link :to="{name:'products-edit', params: {id: 2}}">
            <el-button type="text" size="small">编辑</el-button>
          </router-link>
          
        </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { fetchList } from '@/api/category'
const statusMap = {
  '0': {
    'text': '已下架',
    'class': 'danger'
  },
  '1': {
    'text': '已上架',
    'class': 'success'
  }
}
export default {
  name: 'store',
  data() {
    return {
      list: [],
      form: {
        keyword: '',
        check: ''
      },
      loading: false
    }
  },
  created() {
    this.getStoreslist()
  },
  activate(transition) {
    this.getStoreslist()
    this.pre_params = this.$route.query
    if (this.pre_params.user_id) {
      // do something
      // ajax等在渲染组件前的逻辑操作
    }
    transition.next()
  },
  methods: {
    getStoreslist() {
      this.loading = true
      fetchList({}).then(res => {
        this.list = res.data
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    // 重设每页显示数量
    handleSizeChange(val) {
      this.pageMeta.size = val
      this.getStoreslist()
    },
    // 翻页
    handleCurrentChange(val) {
      this.pageMeta.page = val
      this.getStoreslist()
    },
    onSubmit() {
      this.pageMeta.page = 1
      this.pageMeta.size = 15
      this.getStoreslist()
    }
  },
  filters: {
    statusFilter: function([status, type]) {
      return statusMap[status][type]
    }
  }
}
</script>

<style>
.store-table .store-table-expand label {
    width: 90px;
    color: #76838f;
    font-weight: normal;
  }
</style>

<style lang="scss" scoped>
.store-table .store-table-expand {
  .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 40%;
  }
}
</style>
